<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Models\Safe;
use App\Models\TempInspect;
use App\Models\SafeType;
use Illuminate\Support\Facades\Log;

class SafeService
{
    public function get($search){
        $sql = Safe::with('project')->with('type')->with('user')->when($search, function ($query, $search) {
            return $query->where(function($query) use ($search) {
                $query->where('title','like', '%'.$search.'%');
            });
        });

        return $sql;
    }

    public function insert($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        $title = explode(',',$res['title']);
        $floor = explode(',',$res['floor']);
        $insert = [];
        foreach ($title as $k => $v) {
            foreach ($floor as $kf => $vf) {
                $floor_ = [];
                $floor_['title'] = $v;
                $floor_['floor'] = $vf;
                $floor_['project_id'] = $res['project_id'];
                $floor_['person_id'] = $res['person_id'];
                $floor_['type_id'] = $res['type_id'];
                $floor_['plan_id'] = $res['plan_id'];
                $floor_['status'] = $res['status'] ?? 1;
                $insert[] = $floor_;
            }
        }
        try {
            $data = Safe::insert($insert);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function update($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        unset($res['id']);
        try {
            $data = Safe::where('id',$input['id'])->update($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function getType($search){
        $sql = SafeType::when($search, function ($query, $search) {
            return $query->where(function($query) use ($search) {
                $query->where('title','like', '%'.$search.'%');
            });
        });

        return $sql;
    }

    public function insertType($input){
        
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        
        try {
            SafeType::create($res);
            return true;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function updateType($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        unset($res['id']);
        try {
            $data = SafeType::where('id',$input['id'])->update($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function insertTemp($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        
        try {
            TempInspect::create($res);
            return true;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }
    
    
}